/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Manish Mehta 
 												
Class							:				PeasKilledAvAttUnitsFP
 
Function						: 				A class for detecting the pattern 
												of a peasant getting killed while 
												at the same time having idle attacking 
												units 
****************************************************************************/
package behavioradaptation.failurepattern;


import java.util.Vector;

import base.WargusStateImporter;
import behavioradaptation.analysisdata.AbstractedTrace;
import behavioradaptation.analysisdata.FPSuccessData;
import behavioradaptation.analysisdata.IdleData;
import behavioradaptation.analysisdata.KillData;

public class PeasKilledAvAttUnitsFP{
	String []m_attackUnits = {"footman", "archer","ballista","catapult"};
 
	public Vector<FPSuccessData> detectPattern(AbstractedTrace a_abstractedtrace)
	{
		Vector<KillData> a_killData = a_abstractedtrace.getKillData();
		Vector<IdleData> a_idleData = a_abstractedtrace.getIdleData();
		Vector <FPSuccessData> sucdataList = new Vector<FPSuccessData>();
		for(int i = 0; i < a_killData.size(); i++)
		{
			KillData k_data = (KillData)a_killData.get(i);
			if(k_data.getUnit().getType()==WargusStateImporter.unitTypeToInteger("peasant"))
			{
				System.out.println("AttackStartCycle:" + k_data.getAttackStartCycle());
				System.out.println("KillCycle:" + k_data.getKillCycle());	
				// Find idle attack units when this peasant was killed 
				Vector<IdleData> idleauList = findIdleAttackUnits(a_idleData, k_data.getAttackStartCycle(), k_data.getKillCycle());
				for(int k = 0; k < idleauList.size(); k++)
				{	
					IdleData idata = (IdleData)idleauList.get(k);
					System.out.print("Start Cycle is:" + idata.getStartCycle());
					System.out.print("End Cycle is:" + idata.getEndCycle());
					System.out.println("Unit Type is :" + idata.getUnit().getType());													
				}		
				// Add it to the success list
				if(idleauList.size() != 0)
				{
					FPSuccessData suc_data = new FPSuccessData(k_data, idleauList);
					sucdataList.add(suc_data);
				}	
			}	
		}
		return sucdataList;
	}
	/*
	 * Find Idle Attack Unit when the unit (in this case a peasant) was killed
	 */
	public Vector<IdleData> findIdleAttackUnits(Vector<IdleData> a_idledataList, int a_attackStartCycle, int a_killedAtCycle)
	{
		Vector <IdleData> idleattunitsList = new Vector<IdleData>();
		for(int i = 0; i < a_idledataList.size(); i++)
		{
			IdleData i_data = (IdleData)a_idledataList.get(i);
			for(int k = 0; k < m_attackUnits.length; k++)
			{	
				// check whether the idle interval and the kill intervals match together
				if(WargusStateImporter.unitTypeToString(i_data.getUnit().getType()).equals(m_attackUnits[k]))
				{
					if(((i_data.getStartCycle() < a_attackStartCycle) &&
					   (i_data.getEndCycle() > a_killedAtCycle)) || ((i_data.getStartCycle() > a_attackStartCycle) &&
					    (i_data.getStartCycle() < a_killedAtCycle - 100) &&	   
					   (i_data.getEndCycle() > a_killedAtCycle)))
					{
						idleattunitsList.add(i_data);
					}	
				}	
			}	
		}	
		return idleattunitsList;
	}
	
}
